package com.apes.framework.jpa.generator.model;

import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.jpa.generator.Implementation;
//import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.framework.plugin.outbox.annotation.OutBoxExclude;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.Set;

/**
 * 功能：序列
 *
 * @author xuliang
 * @create 2018-02-04 14:24
 */
@OutBoxExclude
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = Sequence.CONST_TABLE_NAME, indexes = {@Index(name = "ir_sequence_code", columnList = "code")})
public class Sequence extends Model {
    public static final String CONST_TABLE_NAME = "ir_sequence";

    //    @SequenceGenerator(name = CONST_TABLE_NAME, sequenceName = CONST_TABLE_NAME + "_SEQ")
//    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = CONST_TABLE_NAME)
    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "序列")
    private long id;

    @Column(name = "implementation", nullable = false, length = 60)
    private String implementation = Implementation.STANDARD.toString(); //实现：('standard', '标准'), ('no_gap', '无间隔')

    @Column(name = "name", nullable = false, length = 60)
    private String name; //名称

    @Column(name = "code", length = 60)
    private String code; //序列代码

    @Column(name = "valid", nullable = false)
    private boolean valid = true; //有效

    @Column(name = "prefix", length = 30)
    private String prefix; //前缀

    @Column(name = "suffix", length = 30)
    private String suffix; //后缀

    @Column(name = "padding", nullable = false)
    private int padding = 1; //序列长度

    @Column(name = "number_increment", nullable = false)
    private int numberIncrement = 1; //步

    @Column(name = "number_next", nullable = false)
    private int numberNext = 1; //下一号码

//    @ManyToOne(fetch = FetchType.LAZY)
//    @JoinColumn(name = "company_id", referencedColumnName = "id")
//    private PartyRole company; //公司

    @Column(name = "company_id", length = 30)
    private Long company; //公司

    @Column(name = "use_date_range", nullable = false)
    private boolean useDateRange = false;

    @OneToMany(mappedBy = "sequence", fetch = FetchType.LAZY)
    private Set<SequenceDateRange> dateRangeIds; //子序列

    public void addNumberNext() {
        this.numberNext += numberIncrement;
    }
}
